package com.atguigu.tingshu.user.pattern.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.pattern.DeliveryPolicy;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component("1003")
public class VIPDeliveryPolicy implements DeliveryPolicy {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserVipServiceMapper userVipServiceMapper;
    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Override
    public void delivery(UserPaidRecordVo userPaidRecordVo) {
        //3.1 根据订单编号查询是否存在购买记录，如果存在则忽略
        Long count = userVipServiceMapper.selectCount(
                new LambdaQueryWrapper<UserVipService>()
                        .eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo())
                        .select(UserVipService::getId)
        );
        if (count > 0) {
            return;
        }
        //3.2 新增会员购买记录
        UserVipService userVipService = new UserVipService();
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        //3.2.1 获取当前用户身份，判断是否为VIP
        Boolean isVIP = false;
        UserInfoVo userInfoVo = new UserInfoVo();
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        if (userInfo != null) {
            userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVo.class);
        }
        if (userInfoVo.getIsVip().intValue() == 1 && userInfoVo.getVipExpireTime().after(new Date())) {
            isVIP = true;
        }
        //3.2.2 获取用户购买套餐信息，得到套餐服务月
        Long vipID = userPaidRecordVo.getItemIdList().get(0);
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipID);
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        //3.2.3 本次会员生效时间
        if (!isVIP) {
            // 如果是普通用户，生效时间为当前时间
            userVipService.setStartTime(new Date());
            //3.2.4 本次会员到期时间
            userVipService.setExpireTime(DateUtil.offsetMonth(new Date(), serviceMonth));
        } else {
            // 如果是VIP用户
            // 生效时间=现有会员到期时间+1天
            userVipService.setStartTime(DateUtil.offsetDay(userInfoVo.getVipExpireTime(), 1));
            userVipService.setExpireTime(DateUtil.offsetMonth(userVipService.getStartTime(), serviceMonth));
        }
        userVipServiceMapper.insert(userVipService);

        //3.3 更新用户表会员标识以及会员到期时间
        userInfo.setId(userPaidRecordVo.getUserId());
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
